﻿using Centny.Data;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Centny.Test;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Input;
using Windows.UI;
using System.ComponentModel;
// 分割頁項目範本已記錄在 http://go.microsoft.com/fwlink/?LinkId=234234

namespace Centny
{
    /// <summary>
    /// 顯示群組標題、群組內之項目清單和
    /// 目前選取項目之詳細資料的頁面。
    /// </summary>
    public sealed partial class SplitPage : Centny.Common.LayoutAwarePage, INotifyPropertyChanged
    {
        public SplitPage()
        {
            this.InitializeComponent();

        }

        /// <summary>
        /// 巡覽期間以傳遞的內容填入頁面。從之前的工作階段
        /// 重新建立頁面時，也會提供儲存的狀態。
        /// </summary>
        /// <param name="navigationParameter">最初要求這個頁面時，傳遞到
        /// <see cref="Frame.Navigate(Type, Object)"/> 的參數。
        /// </param>
        /// <param name="pageState">這個頁面在先前的工作階段期間保留的
        /// 狀態字典。第一次瀏覽頁面時，這一項是 null。</param>
        /// 
        String Parameter;
        AutoCanvas ACanvas;
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {

            this.Parameter = navigationParameter as String;
            this.Loaded += OnLoaded;
            //this.OnLoaded(null, null);
            //this.ACanvas.InitialElements();

        }
        public void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.ACanvas = new AutoCanvas();
            if ("AutoHor".Equals(Parameter))
            {
                this.ACanvas.TBuilder.Source = new Uri("ms-appx:///Test/AutoHorTemplate.xaml");
                this.ACanvas.LType = AutoCanvas.LayoutType.Horizontal;
                //this.ACanvas.TemplateBuilder.OnRandomElement = RandomHandler;
                //this.ACanvas.Scrollable = false;
            }
            if ("AutoRandom".Equals(Parameter))
            {
                this.ACanvas.TBuilder.Source = new Uri("ms-appx:///Test/AutoRandomTemplate.xaml");
                this.ACanvas.LType = AutoCanvas.LayoutType.Random;
            }
            if ("AutoTime".Equals(Parameter))
            {
                this.ACanvas.TBuilder.Source = new Uri("ms-appx:///Test/AutoTimeTemplate.xaml");
                this.ACanvas.LType = AutoCanvas.LayoutType.Random;
                this.ACanvas.ItemPadding = new Thickness(0, 0, 0, 0);
            }
            if ("AutoWaterfallHor".Equals(Parameter))
            {
                this.ACanvas.TBuilder.Source = new Uri("ms-appx:///Test/AutoWaterfallTemplate.xaml");
                this.ACanvas.LType = AutoCanvas.LayoutType.WaterfallHorizontal;
                this.ACanvas.TBuilder.OnRandomElement = RandomHandler;
                this.ACanvas.TBuilder.OnRandomGroup = RandomGroupHandler;
            }
            if ("AutoWaterfallVer".Equals(Parameter))
            {
                this.ACanvas.TBuilder.Source = new Uri("ms-appx:///Test/AutoWaterfallTemplate.xaml");
                this.ACanvas.LType = AutoCanvas.LayoutType.WaterfallVertical;
            }
            this.ACanvas.TargetOffset = 700;
            this.ACanvas.TBuilder.OnCreateElement+= OnCreateHandler;
            this.ACanvas.OnElementTapped += OnElementPointerPressed;
            this.ACanvas.ItemSources = new TestDataSource().RandomTestData(200);
            this.ACanvas.Background = new SolidColorBrush(Color.FromArgb(255, 255, 9, 9));
            this.Container.Content = this.ACanvas;
            this.Container.UpdateLayout();

        }
        public List<String> RandomGroupHandler(Centny.AutoCanvas.DataTemplateBuilder builder, Random rd, Double ext)
        {
            FrameworkElement ui = builder["T1"] as FrameworkElement;
            Double Height = ui.Height;
            List<String> keys = new List<string>();
            if (Height > ext)
            {
                return keys;
            }
            else
            {
                keys.Add(Height + "");
                return keys;
            }
        }
        public FrameworkElement RandomHandler(Centny.AutoCanvas.DataTemplateBuilder builder, IList<Object> all, Object current, Random rd, Object ext)
        {
            FrameworkElement ui = builder["T1"] as FrameworkElement;
            return ui;
        }

        public void OnCreateHandler(AutoCanvas.DataTemplateBuilder builder, FrameworkElement ui, IList<Object> all, Object current, Object ext)
        {
            //ui.Width = 300;
        }
        public void OnElementPointerPressed(Object item, Object sender, TappedRoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Type:" + item.GetType());
        }
        public void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
